package org.dizena.quartz.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.dizena.common.interaction.request.PageRequest;
import org.dizena.quartz.bean.cmd.AddGroupJobCmd;
import org.dizena.quartz.bean.cmd.AddSingleJobCmd;
import org.dizena.quartz.bean.cmd.JobCmd;
import org.dizena.quartz.bean.dto.QuartzJobDetail;
import org.dizena.quartz.service.QuartzJobService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/job")
@Tag(name = "任务接口")
public class QuartzJobController {

    @Resource
    private QuartzJobService quartzJobService;

    @Operation(summary = "新增单任务")
    @ApiOperationSupport(order = 101)
    @PostMapping("/addSingleJob")
    public void addSingleJob(@RequestBody AddSingleJobCmd cmd) {
        quartzJobService.addSingleJob(cmd);
    }


    @Operation(summary = "修改单任务")
    @ApiOperationSupport(order = 102)
    @PostMapping("/updateSingleJob")
    public void updateSingleJob(@RequestBody AddSingleJobCmd cmd) {
        quartzJobService.updateSingleJob(cmd);
    }

    @Operation(summary = "新增组任务")
    @ApiOperationSupport(order = 111)
    @PostMapping("/addGroupJob")
    public void addGroupJob(@RequestBody AddGroupJobCmd cmd) {
        quartzJobService.addGroupJob(cmd);
    }


    @Operation(summary = "修改组任务")
    @ApiOperationSupport(order = 112)
    @PostMapping("/updateGroupJob")
    public void updateGroupJob(@RequestBody AddGroupJobCmd cmd) {
        quartzJobService.updateGroupJob(cmd);
    }

    @Operation(summary = "任务查询")
    @ApiOperationSupport(order = 113)
    @PostMapping("/listJob")
    public Page<QuartzJobDetail> listJob(@RequestBody PageRequest qry) {
       return quartzJobService.listJob(qry);
    }

    @Operation(summary = "立即执行")
    @ApiOperationSupport(order = 121)
    @PostMapping("/runNow")
    public void runNow(@RequestBody JobCmd cmd) {
        quartzJobService.runNow(cmd);
    }

    @Operation(summary = "暂停任务")
    @ApiOperationSupport(order = 122)
    @PostMapping("/pause")
    public void pauseJob(@RequestBody JobCmd cmd) {
        quartzJobService.pauseJob(cmd);
    }

    @Operation(summary = "恢复任务")
    @ApiOperationSupport(order = 123)
    @PostMapping("/resume")
    public void resumeJob(@RequestBody JobCmd cmd) {
        quartzJobService.resumeJob(cmd);
    }

    @Operation(summary = "删除任务")
    @ApiOperationSupport(order = 125)
    @PostMapping("/delete")
    public void deleteJob(@RequestBody JobCmd cmd) {
        quartzJobService.deleteJob(cmd);
    }

    @Operation(summary = "暂停所有任务")
    @ApiOperationSupport(order = 126)
    @GetMapping("/pauseAll")
    public void pauseAllJobs() {
        quartzJobService.pauseAllJobs();
    }

    @Operation(summary = "恢复所有任务")
    @ApiOperationSupport(order = 127)
    @GetMapping("/resumeAll")
    public void resumeAllJobs() {
        quartzJobService.resumeAllJobs();
    }

    @Operation(summary = "开启所有任务")
    @ApiOperationSupport(order = 128)
    @GetMapping("/startAll")
    public void startAllJobs() {
        quartzJobService.startAllJobs();
    }

    @Operation(summary = "关闭所有任务")
    @ApiOperationSupport(order = 129)
    @GetMapping("/closeAll")
    public void shutdownAllJobs() {
        quartzJobService.shutdownAllJobs();
    }


}
